/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.taglib.head.css;


/**
 * class that contains the information needed to generate a css link.
 */
public final class CssLink
    implements Comparable<CssLink>
{
  
  /**
   * default CSS media used as marker.
   */
  public static final String DEFAULT_CSS_MEDIA = "_DEFAULT_CSS_MEDIA_";
  
  /**
   * the CSS link.
   */
  private final transient String link;

  /**
   * the media of the css.
   */
  private final transient String media;

  /**
   * spring name where to get the UrlManager.
   */
  private final transient String urlManagerName;

  /**
   * the priority of the css link.
   */
  private int priority;



  /**
   * constructor.
   * 
   * @param link
   *          the CSS link
   * @param media
   *          the media of the css
   * @param urlManagerName
   *          spring name where to get the UrlManager
   * @param priority
   *          the priority of the css link
   */
  public CssLink(final String link, final String media, final String urlManagerName, int priority)
  {
    this.link = link;
    this.media = media == null ? DEFAULT_CSS_MEDIA : media;
    this.urlManagerName = urlManagerName;
    this.priority = priority;
  }



  /**
   * comparaison method.
   * 
   * @param cssLink
   *          the other css link
   * @return the value <code>0</code> if the css link is equal to this string;
   *         a value less than <code>0</code> if this css link has a lesser
   *         priority than the css link argument; and a value greater than
   *         <code>0</code> if this css link has a greater priority than the
   *         css container argument.
   */
  public int compareTo(final CssLink cssLink)
  {
    int res = getMedia().compareTo(cssLink.getMedia());
    if (res != 0) {
      return res;
    }    
    res = getUrlManagerName().compareTo(cssLink.getUrlManagerName());
    if (res != 0) {
      return res;
    }
    res = getLink().compareTo(cssLink.getLink());
    if (res != 0) {
      return res;
    }
    if (getPriority() > cssLink.getPriority()) {
      setPriority(cssLink.getPriority());
    }
    else {
      cssLink.setPriority(getPriority());
    }
    return 0;
  }



  /**
   * getter for the link property.
   * 
   * @return the link
   */
  public String getLink()
  {
    return link;
  }



  /**
   * getter for the urlManagerName property.
   * 
   * @return the urlManagerName
   */
  public String getUrlManagerName()
  {
    return urlManagerName;
  }



  /**
   * getter for the priority property.
   * 
   * @return the priority
   */
  public int getPriority()
  {
    return priority;
  }



  /**
   * setter for the priority property.
   * 
   * @param priority
   *          the priority to set
   */
  public void setPriority(int priority)
  {
    this.priority = priority;
  }



  /**
   * getter for the media property.
   * 
   * @return the media
   */
  public String getMedia()
  {
    return media;
  }
}
